package chapter2;

import chapter2.version1.AppleVersionOne;
import chapter2.version2.ApplePredicate;
import chapter2.version2.AppleRedAndHeavyPredicate;
import chapter2.version2.AppleVersionTwo;

import java.util.ArrayList;
import java.util.List;

public class DemoTest {
    public static void main(String[] args) {
        ArrayList inventory = new ArrayList<Apple>();
        inventory.add(new Apple("green",80));
        inventory.add(new Apple("green", 155));
        inventory.add(new Apple("red", 120));
        List<Apple> greenApples = AppleVersionOne.filterAppleByColor(inventory,"green");
        List<Apple> redApples = AppleVersionOne.filterAppleByColor(inventory,"red");
        List<Apple> redAndHeavyApples = AppleVersionTwo.filterApples(inventory,new AppleRedAndHeavyPredicate());

        /**
         * 使用匿名类
         * 直接内联参数化filterapples方法的行为
         *  匿名类还是不够好 还是包含很多的模板代码 太笨重 占用了很多空间
         */
        List<Apple> redApples2 = AppleVersionTwo.filterApples(inventory, new ApplePredicate() {
            @Override
            public boolean test(Apple apple) {
                return "red".equals(apple.getColor());
            }
        });

        /**
         * 使用Lambda表达式
         */
        List<Apple> result = AppleVersionTwo.filterApples(inventory, (Apple apple) -> "red".equals(apple.getColor()));
    }

}
